// pages/mouthDetail/mouthDetail.js
import {
  hgetStorage,
  hshowToast,
  hnavigateBack
} from "../../static/htools.wx";
import {
  hrequestGet,
  hrequestPut
} from "../../static/hunit.wx";
import api from '../../assets/api'
const {
  DeclareMouthId,
  updatedeclareMonth
} = api
import {
  hyearMouth,
  hmouth
} from '../../static/handleTimes'
import WxValidate from '../../static/WxValidate'
Page({
  data: {
    queryId: '',
    curMouth: '',
    forecastNumber: '',
    adjustmentNumber: '',
    declareMonthDto: {},
    declareMonthDtoLast: {},
    number : ""
  },
  onLoad(options) {
    this.initValidate() //验证规则函数
    this.setData({
        queryId: options.id
      }),
      this.getMouthDataById()
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  initValidate() {
    const rules = {
      forecastNumber: {
        required: true,
        number: true
      },
      adjustmentNumber: {
        required: true,
        number: true
      }
    }
    const messages = {
      forecastNumber: {
        required: '请输入预估电量',
        number: '请输入数字'
      },
      adjustmentNumber: {
        required: '请输入调整电量',
        number: '请输入数字'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  // 表单验证并提交申报
  async formSubmit(e) {
    let params = e.detail.value
    if (this.data.declareMonthDtoLast === null) {
      params.adjustmentNumber = 0
    } else {
      params.adjustmentNumber = this.data.adjustmentNumber
    }
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      console.log(this.WxValidate.errorList)
      this.showModal(error)
      return false
    }
    let _dataJson = []
    let _dataJsonOne = {
      actualNumber: this.data.declareMonthDto.actualNumber == null ? '' : this.data.declareMonthDto.actualNumber,
      adjustmentNumber: this.data.declareMonthDto.adjustmentNumber == null ? '' : this.data.declareMonthDto.adjustmentNumber,
      correctNumber: this.data.declareMonthDto.correctNumber == null ? '' : this.data.declareMonthDto.correctNumber,
      //declareMonth: this.data.declareMonthDto.declareMonth == null ? '' : this.data.declareMonthDto.declareMonth,
      forecastNumber: this.data.forecastNumber,
      id: this.data.declareMonthDto.id,
      deptId: hgetStorage("deptId"),
      status: 1
    }
    console.log(_dataJsonOne);
    if (this.data.declareMonthDtoLast === null) {
      _dataJson.push(_dataJsonOne)
    } else {
      let _dataJsonTwo = {
        actualNumber: this.data.declareMonthDtoLast.actualNumber == null ? '' : this.data.declareMonthDtoLast.actualNumber,
        adjustmentNumber: this.data.adjustmentNumber,
        correctNumber: this.data.declareMonthDtoLast.correctNumber == null ? '' : this.data.declareMonthDtoLast.correctNumber,
        //declareMonth: this.data.declareMonthDtoLast.declareMonth == null ? '' : this.data.declareMonthDtoLast.declareMonth,
        forecastNumber: this.data.declareMonthDtoLast.forecastNumber == null ? '' : this.data.declareMonthDtoLast.forecastNumber,
        id: this.data.declareMonthDtoLast.id,
        deptId: hgetStorage("deptId"),
        status: 1
      }
      _dataJson.push(_dataJsonOne)
      _dataJson.push(_dataJsonTwo)
    }
    console.log(_dataJson);
    let _res = await hrequestPut(updatedeclareMonth, JSON.stringify(_dataJson))
    console.log(_res);
    if (_res.statusCode === 204) {
      hshowToast('提交成功')
      let pages = getCurrentPages(); //定义上页面
      let prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage) {
        //上一页存在则给上一个data赋值。则把title传过去
        prevPage.setData({
          isMouthUpdate: true,
          dayPage:0,
          isShowMouthMore:true
        });
      }
      hnavigateBack(1)
    } else {
      hshowToast('数据过长请重新输入提交')
    }
  },
  // 根据id 查询月度数据
  async getMouthDataById() {
    let _id = this.data.queryId;
    let _res = await hrequestGet(DeclareMouthId + _id);
    console.log(_res);
    let declareMonthDto = _res.data.declareMonthDto;
    console.log(declareMonthDtoLast);
    
    _res.data.declareMonthDto.yearMouth = hyearMouth(_res.data.declareMonthDto.declareMonth);
    _res.data.declareMonthDto.curMouth = hmouth(_res.data.declareMonthDto.declareMonth);
    let declareMonthDtoLast = _res.data.declareMonthDtoLast == null ? null : _res.data.declareMonthDtoLast;
    this.setData({
      declareMonthDto,
      declareMonthDtoLast,
      forecastNumber:declareMonthDto.forecastNumber,
      adjustmentNumber:!declareMonthDtoLast ? null:declareMonthDtoLast.adjustmentNumber,
      number : _res.data.number
    })
    console.log(this.data.declareMonthDtoLast);
    
  },
  forecastNum(e) {
    this.setData({
      forecastNumber: e.detail.value
    })
  },
  adjustNum(e) {
    this.setData({
      adjustmentNumber: e.detail.value
    })
  }
})